const userModel = require('../models/userModel')
const jwt = require('jsonwebtoken');
const fs = require('fs')
const { HOST } = require('../config/server')

// 登录
module.exports.login = async (req, res, next) => {
    let userInfo = {
        username: req.body.username,
        password: req.body.password
    }
    try {
        let info = await userModel.findOne(userInfo)
        if (info) {
            let token = jwt.sign({
                username: userInfo.username
            }, 'sujing', {
                expiresIn: '1d'
            })
            delete info.password
            res.send({
                code: 200,
                msg: '登录成功',
                data: {
                    token,
                    info
                }
            })
            return
        }
        res.send({
            code: 500,
            msg: '登录失败, 登录或密码有误'
        })
    } catch (error) {
        console.log(error);
        res.send({
            code: 501,
            msg: '登录失败, 登录或密码有误',
        })
    }
}

// 注册
module.exports.register = async (req, res, next) => {
    try {
        if (await userModel.findOne({ username: req.username })) return res.send({
            code: 500,
            data: '账号已存在，请换一个'
        })
        let createResult = await userModel.create(req.body)
        if (createResult) {
            res.send({
                code: 200,
                data: '创建成功',
            })
            return
        }
        res.send({
            code: 500,
            data: '创建失败，参数有误'
        })
    } catch (error) {
        next(error)
    }
}

// 修改资料
module.exports.updateInfo = async (req, res, next) => {
    try {
        delete req.body.username;
        delete req.body.password;
        delete req.body._id;
        let result = await userModel.updateOne({ username: req.auth.username }, req.body)
        if (result) return res.send({
            code: 200,
            data: '修改成功'
        })
        res.send({
            code: 500,
            data: '修改失败'
        })
    } catch (error) {
        next(error)
    }
}

// 获取资料
module.exports.getUserInfo = async (req, res, next) => {
    try {
        // 查询数据库，且删除敏感字段
        let result = await userModel.findOne({ username: req.auth.username }, { _id: 0, password: 0, __v: 0 })
        if (result) return res.send({
            code: 200,
            msg: '获取成功',
            data: result
        })
        res.send({
            code: 500,
            data: '获取失败'
        })
    } catch (error) {
        next(error)
    }
}

// 修改密码
module.exports.resetPassword = async (req, res, next) => {
    try {
        let result = await userModel.updateOne({ username: req.auth.username }, { password: req.body.password })
        if (result) return res.send({
            code: 200,
            data: '修改成功'
        })
        res.send({
            code: 500,
            data: '修改失败'
        })
    } catch (error) {
        next(error)
    }
}

// 上传头像
module.exports.uploadAvatar = async (req, res, next) => {
    // 删除历史的头像
    // fs.unlink('./public/avatar/' + req.file.filename, () => { })
    let result = await userModel.findOneAndUpdate({ username: req.auth.username }, { avatar: req.avatarUrl })
    if (result) return res.send({
        code: 200,
        data: '上传成功',
        url: HOST + '/public/avatar/' + req.file.filename
    })
    res.send({
        code: 200,
        data: '上传成功',
    })
}
